#include "ParserXML.h"

ParserXML::ParserXML()
{
}

ParserXML::~ParserXML()
{
}

std::string ParserXML::generarXML(std::vector<tAtribVariables> vars,std::string raiz)
{
	std::string retorno;
	TiXmlDocument doc;
	TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8","");
	doc.LinkEndChild(decl);
	TiXmlElement* root = new TiXmlElement(raiz.c_str());
	doc.LinkEndChild(root);
	TiXmlElement* element;
	std::string valor;
	for(unsigned i=0; i<vars.size(); i++)
	{
		element = new TiXmlElement("Parcela");
		element->SetAttribute("x",vars[i].getX());
		element->SetAttribute("y",vars[i].getY());
		std::stringstream ss;ss << vars[i].getValor();
		ss >> valor;
		element->LinkEndChild(new TiXmlText (valor.c_str()));
		root->LinkEndChild(element);
	}
	std::string str;
	TiXmlPrinter printer;
	//printer.SetIndent ("");
	//printer.SetLineBreak("");
	//printer.SetStreamPrinting();
	doc.Accept(&printer);
	str.assign(printer.CStr());
	return str;
}

void ParserXML::leerXML(std::string documento,std::vector<tAtribVariables>& vectorAtrib)
{
	TiXmlDocument doc;
	doc.Parse(documento.c_str());
	tAtribVariables atribVar;
	int x, y;
	TiXmlElement *raiz = doc.FirstChildElement();
	TiXmlNode* nodo = raiz->FirstChild();
	TiXmlElement* element;
	while(nodo)
	{
		element = nodo->ToElement();
		element->QueryIntAttribute("x",&x);
		atribVar.setX(x);
		element->QueryIntAttribute("y",&y);
		atribVar.setY(y);
		atribVar.setValor(atof(nodo->FirstChild()->Value()));
		vectorAtrib.push_back(atribVar);
		nodo = nodo->NextSibling();
	}
}

